import { deepClone } from "@inbiz/utils";

// file流转 base64
export const getBase64 = (file: File) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
};

// blob转Base64
export const blobToBase64 = (url: any) => {
  return new Promise((resolve, reject) => {
    const image = new Image();
    image.src = url;
    image.onload = () => {
      const canvas = document.createElement('canvas');
      canvas.width = image.width;
      canvas.height = image.height;
      const context = canvas.getContext('2d');
      context?.drawImage(image, 0, 0, image.width, image.height);
      const imgUrl = canvas.toDataURL(); //图片的base64地址
      resolve(imgUrl);
    };
    image.onerror = (error) => reject(error);
  });
};

// 预览绘制canvas 并转成图片
export const getCroppedImg = (image: any, crop: any, fileName: any) => {
  let fileUrl: any = null;
  const canvas = document.createElement('canvas');
  const scaleX = image.naturalWidth / image.width;
  const scaleY = image.naturalHeight / image.height;
  canvas.width = crop.width;
  canvas.height = crop.height;
  const ctx = canvas.getContext('2d');
  ctx?.drawImage(
    image,
    crop.x * scaleX,
    crop.y * scaleY,
    crop.width * scaleX,
    crop.height * scaleY,
    0,
    0,
    crop.width,
    crop.height,
  );
  return new Promise((resolve, reject) => {
    canvas.toBlob((blob) => {
      if (!blob) {
        console.error('Canvas is empty');
        return;
      }
      blob.name = fileName;
      window.URL.revokeObjectURL(fileUrl);
      fileUrl = window.URL.createObjectURL(blob);
      resolve(fileUrl);
    }, 'image/png');
  });
};

export function replaceTheme(theme: any, personFont?: number) {
  let newTheme = deepClone(theme);
  let oldTheme: any = null;
  const mainFontSize = '--inbiz-font-size-base'; //主字体大小
  const topMenuFontSize = '--inbiz-font-size-menu-top'; //主字体大小
  const leftMenuFontSize = '--inbiz-font-size-menu-left'; //主字体大小
  let isFontSize = false;
  let menuArray = [];
  const setThemeItem = (themeItem: any) => {
    isFontSize =
      themeItem.id == mainFontSize ||
      themeItem.id == topMenuFontSize ||
      themeItem.id == leftMenuFontSize;
    if (isFontSize && typeof personFont == 'number') {
      return {
        ...themeItem,
        fontSize: personFont + 'px',
        size: personFont,
      };
    }
    return themeItem;
  };

  if (theme) {
    Object.keys(theme).forEach((key: string) => {
      oldTheme = theme[key];
      if (Array.isArray(oldTheme)) {
        newTheme[key] = oldTheme.map((themeItem: any) => {
          return setThemeItem(themeItem);
        });
      } else {
        menuArray = Object.keys(oldTheme);
        menuArray.forEach((menuKey: any) => {
          newTheme[key][menuKey] = oldTheme[menuKey].map((themeItem: any) => {
            const newData = setThemeItem(themeItem);
            return newData;
          });
        });
      }
    });
  }
  return newTheme;
}